package io.renren.utils;

import io.renren.entity.SocialSecurityAreaBasicEntity;
import io.renren.entity.SocialSecurityPersonBasicEntity;

import java.lang.reflect.Field;

/**
 * Created by zhangheng on 2017/7/11.
 */
public class ReflectUtil {

    public static SocialSecurityPersonBasicEntity setSSPersonBasic(SocialSecurityAreaBasicEntity areaBasic,
                                                                   SocialSecurityPersonBasicEntity ssperson,String basicflag) {
        Class areaBasicClass = areaBasic.getClass();
        Field[] fs = areaBasicClass.getDeclaredFields();
        for (Field f : fs) {
            f.setAccessible(true);
            String name = f.getName();
            Object val = null;
            try {
                val = f.get(areaBasic);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            Class sspersonClass = ssperson.getClass();
            Field[] fsssperson = sspersonClass.getDeclaredFields();
            for (Field field : fsssperson) {
                field.setAccessible(true);
                String ssname = field.getName();
                if (!ssname.equals("id")) {
                    if (ssname.equals(name)) {
                        System.out.print(name);
                        if("0".equals(basicflag)){
                            if(name.contains("_basic")){
                                continue;
                            }else{
                                try {
                                    field.set(ssperson, val);
                                    break;
                                } catch (IllegalAccessException e) {
                                    e.printStackTrace();
                                }
                            }
                        }else{
                            try {
                                field.set(ssperson, val);
                                break;
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                }
            }
        }
    return ssperson;
    }


    public static Object reflectClass(Object local, Object n){
        Class localClass = local.getClass();
        Field[] fslocal = localClass.getDeclaredFields();
        for (Field field : fslocal) {
            field.setAccessible(true);
            String name = field.getName();
            if(!"year".equals(name)&&!"month".equals(name)&&!"serialVersionUID".equals(name)
                    &&!"id".equals(name)&&!"index".equals(name)){
                Class nclass = n.getClass();
                Field[] fsn = nclass.getDeclaredFields();
                for (Field f : fsn) {
                    f.setAccessible(true);
                    String fname = f.getName();
                    if(fname.equals(name)){
                        try {
                            f.set(n,field.get(local));
                            break;
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }

        }
        return n;
    }
}
